﻿using System.Text;

namespace etcetera.Mvc.UI.Progressbar
{
    /// <summary>
    /// 
    /// </summary>
    public interface IProgressbar
    {
        IProgressbar WithId(string id);
        IProgressbar WithColor(string color);
    }

    /// <summary>
    /// 
    /// </summary>
    public class Progressbar : IProgressbar
    {
        private float _percentage;
        private string _id;
        private string _color;

        public Progressbar(float percentage)
        {
            _percentage = percentage;
        }

        public IProgressbar WithId(string id)
        {
            _id = id;
            return this;
        }

        public IProgressbar WithColor(string color)
        {
            _color = color;
            return this;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            // TODO attributes, define standard class

            /*
            <div class="progress" id="prog1">
                <div class="progbar" style='width:50%;'></div>
            </div>
            
            Style Sheet:
            
            .progress
            {
              border: 1px solid #ccc;
              height: 16px;
            }

            .progbar
            {
              background-color: #afa;
              height: 16px;
            }
            */

            string id = _id == null ? string.Empty : string.Format("id=\"{0}\" ", _id);
            sb.AppendLine(string.Format("<div {0}class='{1}'>", id, "progress"));
            
            string color = string.IsNullOrEmpty(_color) ? string.Empty : string.Format(" background-color: {0};", _color);

            if (_percentage > 0.0f)
            {
                sb.AppendLine(string.Format("<div class='{0}' style='width:{1}%;{2}'>", "progbar", _percentage, color));
                sb.AppendLine("</div>"); // For some strange reason the whole layout falls apart when using a self closing <div />
            }

            sb.AppendLine("</div>");

            return sb.ToString();
        }
    }
}
